package com.dlg.exchange.engin.config;

import com.dlg.common.bus.MqttBusSender;
import com.dlg.common.checksum.ByteCheckSum;
import com.dlg.common.checksum.ICheckSum;
import com.dlg.common.codec.BodyCodecImpl;
import com.dlg.common.codec.IBodyCodec;
import com.dlg.common.codec.IMsgCodec;
import com.dlg.common.codec.MsgCodec;
import com.dlg.common.hq.MatchData;
import com.dlg.exchange.engin.db.DbQuery;
import com.dlg.exchange.engin.handler.BaseHandler;
import com.dlg.exchange.engin.handler.risk.ExistRiskHandler;
import com.dlg.exchange.engin.orderbook.GOrderBookImpl;
import com.dlg.exchange.engin.orderbook.IOrderBook;
import com.google.common.collect.Lists;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import io.vertx.core.Vertx;
import org.apache.commons.dbutils.QueryRunner;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.api.set.primitive.MutableLongSet;
import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ShortObjectHashMap;
import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class EnginConfig {

    @Bean
    Vertx vertx(){
       return Vertx.vertx();
    }

    @Bean
    IBodyCodec bodyCodec(){
        return new BodyCodecImpl();
    }

    @Bean
    ICheckSum cs(){
        return  new ByteCheckSum();
    }

    @Bean
    IMsgCodec msgCodec(){
        return  new MsgCodec();
    }

    @Bean
    DbQuery db(){
        return new DbQuery(runner());
    }

    @Bean
    QueryRunner runner(){
        return new QueryRunner(dataSource());
    }
    @Bean
     ComboPooledDataSource dataSource() {
        return new ComboPooledDataSource();
    }

     @Bean
     IntObjectHashMap<IOrderBook> orderBookMap() throws Exception {
         IntObjectHashMap<IOrderBook> orderBookMap = new IntObjectHashMap<>();
         db().queryAllStockCode().forEach(code -> orderBookMap.put(code, new GOrderBookImpl(code)));
         return orderBookMap;
     }

     @Bean
    ShortObjectHashMap<List<MatchData>> matcherEventMap() throws Exception {

         ShortObjectHashMap<List<MatchData>> matcherEventMap = new ShortObjectHashMap<>();
         for (short id : db().queryAllMemberIds()) {
             matcherEventMap.put(id, Lists.newArrayList());
         }
        return matcherEventMap;
    }

    @Bean
    MutableLongSet uidSet() throws Exception {
        return db().queryAllBalance().keySet();
    }

    @Bean
    MutableIntSet codeSet() throws Exception {
        return  db().queryAllStockCode();
    }


    //2.撮合处理器(订单簿*****) 撮合/提供行情查询
//    @Bean
//    BaseHandler matchHandler() throws Exception {
//        IntObjectHashMap<IOrderBook> orderBookMap = new IntObjectHashMap<>();
//        db().queryAllStockCode().forEach(code -> orderBookMap.put(code, new GOrderBookImpl(code)));
//     return new StockMatchHandler(orderBookMap);
//    }

}
